/**
 *
 */
package com.ybkj.daijia.api.driver.v1;

import com.ybkj.daijia.Version;
import com.ybkj.daijia.api.driver.ErrorConstant;
import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.api.driver.transport.VersionTransport;
import com.ybkj.daijia.server.model.AppVersion;
import com.ybkj.daijia.server.model.AppVersion.AppVersionType;
import com.ybkj.daijia.server.service.AppVersionService;
import com.ybkj.daijia.server.service.FinancialRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author bo
 *
 */
@Controller
@RequestMapping(value = "driver/api/rest/v1")
public class VersionController {

    private static final Logger logger = LoggerFactory.getLogger(VersionController.class);

    @Autowired
    private AppVersionService appVersionService;

    @Autowired
    private FinancialRecordService financialRecordService;


    /**
     * 检查当前APP版本是否为最新
     *
     * @param version
     * @return
     */
    @RequestMapping(value = "checkVersion")
    public @ResponseBody
    VersionTransport checkVersion(float version) {

        float currentVersion = Version.getVersion();

        VersionTransport vt = new VersionTransport();
        vt.setVersion(currentVersion);
        vt.setNewest(version >= currentVersion);
        vt.setIntroduce("新版本介绍");

        return vt;

    }

    @RequestMapping(value = "checkVersionV2")
    public @ResponseBody
    ResultTransport checkVersionV2(AppVersionType type, Integer buildversion) {

        logger.debug("recv type:{},buildversion:{}", type, buildversion);

        if (null == type || null == buildversion) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        AppVersion appVersion = appVersionService.findByAppVersionType(type);
        if (null == appVersion) {
            return ResultTransport.getErrorBy(ErrorConstant.APPVERSION_NOT_EXISTS);
        }

        VersionTransport vt = new VersionTransport();
        vt.setAppVersion(appVersion);
        vt.setNewest(buildversion >= appVersion.getBuildversion());

        ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
        instance.setData(vt);
        return instance;

    }

}
